const express = require('express');
const db=require('../utils/db')
//创建一个路由控制器
const router=express.Router()

//某人写的这个模块的相关接口
app.get('/getdata',function(req,res){
    const sql=`SELECT * FROM test LIMIT ${(req.query.pageNo-1)*req.query.pageSize},${req.query.pageSize};`;
    const sql2=`select count(*) as total from test`
    db.query(sql,function(err,data){
        if(err){
            res.send({code:500,msg:err})
        }else{
            db.query(sql2,function(err2,data2){
                if(err2){
                    res.send({code:500,msg:err})
                }else{
                    res.send({code:200,msg:data,total:data2[0].total})
                }
            })
            
        }
    })
})

app.post('/adddata',function(req,res){
    const sql=`insert into test value(null,'${req.body.content}');`

    db.query(sql,function(err,data){
        if(err){
            res.send({code:500,msg:err})
        }else{
            res.send({code:200,msg:'添加成功'})
        }

    })
})
app.post('/deldata',function(req,res){
    const sql=`delete from test where id=${req.body.id};`;
    db.query(sql,function(err,data){
        if(err){
            res.send({code:500,msg:err})
        }else{
            res.send({code:200,msg:'删除成功'})
        }
    })
})

//导出路由
module.exports=router;